package com.wc.alorithm_blue_bridge._二分.完美数对;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

/**
 * @Author congge
 * @Date 2025/1/12 12:02
 * @description
 * https://www.lanqiao.cn/problems/20109/learning/?contest_id=232
 */
public class Main {
    /**
     * 思路：
     * 计数 + 二分
     * 计数, 记录每一个数有几个
     * 存储, 相同个数的存储到一个数组里面
     * 存储有哪些个数的值
     * 二分找到存在 >= 该数的个数
     *
     * 二分找到 该数个数 <= 个数的数的下角标, 记录答案
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 1000010;
    static int[] num = new int[N], b = new int[N];
    static Map<Integer, ArrayList<Integer>> map = new TreeMap<>();
    static int n;

    public static void main(String[] args) {
        n = sc.nextInt();
        // 计数, 记录每一个数有几个
        for (int i = 1; i <= n; i++) {
            int x = sc.nextInt();
            num[x]++;
        }

        // 存储, 相同个数的存储到一个里面
        for (int i = 1; i < N; i++) {
            if (num[i] > 0) {
                ArrayList<Integer> a = map.getOrDefault(num[i], new ArrayList<>());
                a.add(i);
                if (!map.containsKey(num[i])) map.put(num[i], a);
            }
        }
        n = 0;
        long res = 0;
        // 存储有哪些计数的
        for (int x : map.keySet()) b[++n] = x;

        // 遍历存在的数
        for (int i = 1; i < N; i++) {
            if (num[i] > 0) {
                // 二分找到存在 >= 该数的个数
                int l = 1, r = n;
                while (l < r) {
                    int mid = l + r >> 1;
                    if (b[mid] >= i) r = mid;
                    else l = mid + 1;
                }

                if (b[l] >= i) {
                    // 遍历个数多余该数的时候
                    for (int j = l; j <= n; j++) {
                        int x = b[j];
                        ArrayList<Integer> a = map.get(x);

                        // 二分找到 该数个数 <= 个数的数的下角标, 记录答案
                        l = 0;
                        r = a.size() - 1;
                        while (l < r) {
                            int mid = l + r + 1 >> 1;
                            if (a.get(mid) <= num[i]) l = mid;
                            else r = mid - 1;
                        }
                        if (a.get(l) <= num[i]) res += l + 1;
                    }
                }
            }
        }
        out.println(res);
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

